package com.weigj.controller.DeptApp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.weigj.model.BaseData;
import com.weigj.model.CarInfo;
import com.weigj.model.Content;
import com.weigj.model.Dept;
import com.weigj.model.Employee;
import com.weigj.model.HourContract;
import com.weigj.model.HourContractBackSett;
import com.weigj.model.ReserveHour;
import com.weigj.service.DeptAppService;
import com.weigj.service.impl.DeptAppServiceImpl;

/**
 * @author wanglong
 * 
 */
/**
 * @author wanglong
 * 
 */
@Before(Tx.class)
public class DeptAppController extends Controller {
	private static DeptAppService app = new DeptAppServiceImpl();
	static Content c = new Content();

	public void login()
	{
		String json = "{\"id\":\"0\"}";
		StringBuffer sb = new StringBuffer();
		Employee emp = app.loginSuccess(getPara("name"), getPara("password"));
		if (null == emp)
		{
			renderJson(json);
		} else
		{
			renderJson("id", emp.get("EMPLID"));
		}
	}

	public void queryDept()
	{
		System.out.println(getPara("empId"));
		String json = app.queryUserDeptFunc(getPara("empId"));
		renderJson(json);
	}

	public void queryDeptInfo()
	{
		System.out.println(getPara("deptFunc"));
		List<Dept> depts = app.queryUserDepts(getPara("deptFunc"), getPara("empId"));
		renderJson("dept", depts);
	}

	// 发车首页
	public void sendIndex()
	{
		List<ReserveHour> list = app.queryOrderList(getPara("deptId"));
		System.out.println(list.size());
		renderJson("reserve", list);
	}

	public void queryOneOrderByNum()
	{
		List<ReserveHour> list_rh = new ArrayList<ReserveHour>();
		list_rh = app.queryOrderByNum(getPara("orderNum"), getPara("deptId"));
		renderJson("reserve", list_rh);
	}

	public void showReserveDetail()
	{
		ReserveHour rh = new ReserveHour();
		rh = app.queryOneOrderById(getPara("id"));
		renderJson(rh);
	}

	/**
	 * 根据条形码扫描车辆 判断车型与预定单合同是否相符
	 */
	public void queryQrCar()
	{
		Content c = app.checkReserveHourContractCarCanRent(getPara("carMakeCode"), getPara("num"));
		renderJson(c);
	}

	/**
	 * 查询所有可租车辆 包含订单预定车辆
	 */
	public void queryAllCanRentCar()
	{
		List<CarInfo> carList = app.queryCarListCanRent(getPara("deptId"));
		CarInfo car = ReserveHour.dao.queryCarInfoByNum(getPara("reserveNo"));
		carList.add(car);
		c.setCarList(carList);
		renderJson(c);
	}

	/**
	 * 查询一辆车 根据车牌或车架
	 */
	public void queryOneCar()
	{
		String carParam = getPara("carParam");
		CarInfo car = null;
		car = app.queryCarInfo(carParam);
		String km = CarInfo.dao.queryCarKM(car.getBigDecimal("infoid").toString());
		String localName = CarInfo.dao.getCarLocalName(car.getBigDecimal("localid").toString());
		String tcm = CarInfo.dao.getCarType(car.getBigDecimal("infoid").toString());
		c.setLocalName(localName);
		c.setKm(km);
		c.setC(car);
		c.setTcm(tcm);
		renderJson(c);
	}

	public void queryCarByCarNum()
	{
		List<CarInfo> cars = new ArrayList<CarInfo>();
		cars = CarInfo.dao.queryCarVague(getPara("carNum"), getPara("localId"));
		CarInfo car = ReserveHour.dao.queryCarInfoByNum(getPara("reserveNo"));
		cars.add(car);
		c.setCarList(cars);
		renderJson(c);
	}

	/**
	 * 发车
	 */
	public void sendCar()
	{
		boolean hasSend = false;
		String msg = "";
		String code = "000";
		String initKM = getPara("initKM");
		String orderId = getPara("orderId");
		String carInfoLicense = getPara("carInfoLicense");
		String empName=Employee.dao.findEmpNameById(getPara("empId"));
		hasSend = app.sendCar(initKM, orderId, carInfoLicense,empName);
		if (!hasSend)
		{
			msg = "发车错误";
			code = "404";
		}
		c.setMsg(msg);
		c.setCode(code);
		renderJson(c);
	}

	public void queryBaseData()
	{
		String baseType = getPara("baseType");
		List<BaseData> datas = null;
		if (baseType.equals("0"))
		{
			datas = BaseData.dao.queryListByType("减价理由");
		} else if (baseType.equals("1"))
		{
			datas = BaseData.dao.queryListByType("加价理由");
		} else if (baseType.equals("2"))
		{
			datas = BaseData.dao.queryListByType("预付方式");
		}
		c.setDatas(datas);
		renderJson(c);
	}

	public void findContract()
	{
		HourContract hc = HourContract.dao.findRentingContractByCar(getPara("carNum"));
		c.setHc(hc);
		c.setStartTime(hc.get("STARTTIME").toString().substring(0, 16));
		renderJson(c);
	}

	/**
	 * 查询可还车车辆 不支持模糊查询
	 */
	public void queryCanBackCar()
	{
		List<CarInfo> carList = new ArrayList<CarInfo>();
		CarInfo car = CarInfo.dao.queryCanBackCar(getPara("carNum"));
		if (car != null)
		{
			carList.add(car);
		}
		c.setCarList(carList);
		renderJson(c);
	}

	public void backCarInputParam()
	{
		String hourContractId = getPara("hourContractId");
		String empName=Employee.dao.findEmpNameById(getPara("empId"));
		boolean result = app.backCarSuccess(getPara("backCarFlag"),hourContractId, getPara("endDate"), getPara("endKm"), getPara("deptId"),empName);
		System.out.println(result);
		if (result)
		{
			c.setMsg("操作成功");
			c.setCode("000");
		}else{
			c.setMsg("结算出错");
			c.setCode("100");
		}
		renderJson(c);
	}

	public void paymentIndex()
	{
		List<HourContract> hcs = new ArrayList<HourContract>();
		hcs = HourContract.dao.queryHasBackCar(getPara("deptId"));
		if (hcs != null)
		{
			c.setHcList(hcs);
		}
		renderJson(c);
	}

	public void balanceDetail()
	{
		List<HourContractBackSett> hcbs = null;
		HourContractBackSett hcb = null;
		HourContract hc = HourContract.dao.findById(getPara("hourContractId"));
		hcb = HourContractBackSett.dao.findBackSett(hc.getBigDecimal("BACKSETT_ID") + "");
		hcbs = HourContract.dao.queryAllHasBalanceSet(hc.getStr("CONTRACTNUM"));
		if (hc != null)
			c.setHc(hc);
		if (hcbs != null)
			c.setHcbs(hcbs);
		if (hc != null)
			c.setHcb(hcb);
		c.setStartTime(hc.get("STARTTIME").toString().substring(0, 16));
		c.setEndTime(hc.get("ENDTIME").toString().substring(0, 16));
		renderJson(c);
	}

	/**
	 * 提交收款订单。修改结算单
	 */
	public void subPayment()
	{
		String contractId = getPara("contractId");
		String oper = getPara("oper");
		String reason = getPara("reason");
		String money = getPara("money");
		String shouldInput = getPara("shouldInput");
		String paymethod = getPara("paymethod");
		boolean hasInput = app.successInput(contractId, oper, reason, money, shouldInput, paymethod);
		if (hasInput)
		{
			c.setMsg("操作成功");
			c.setCode("000");
		} else
		{
			c.setMsg("操作失败");
			c.setCode("101");
		}
		renderJson(c);
	}

	/**
	 * 换车首页
	 */
	public void changeCarIndex()
	{
		String deptId = getPara("deptId");
		List<HourContract> hcs = null;
		List<String> endTime= new ArrayList<String>();
		hcs = HourContract.dao.queryAllChangeCarContact(deptId);
		for (HourContract hourContract : hcs)
		{
			endTime.add(hourContract.get("ENDTIME").toString().substring(0,16));
		}
		
		if (hcs != null){
			c.setHcList(hcs);
			c.setEndTimes(endTime);
		}
		renderJson(c);
	}
	public void changeCarSelectCar(){
		HourContract hc = HourContract.dao.findById(getPara("contractId"));
		c.setStartTime(hc.get("STARTTIME").toString().substring(0,16));
		c.setEndTime(hc.get("ENDTIME").toString().substring(0,16));
		c.setHc(hc);
		renderJson(c);
	}
	//检查车辆是否可以执行换车操作
	public void checkCanChangeCar(){
		c = app.checkCanChangeCar(getPara("param"));
		HourContract hc = HourContract.dao.findById(getPara("contractId"));
		String carType = CarInfo.dao.getCarType(c.getC().getBigDecimal("INFOID").toString());
		c.setTcm(carType);
		c.setHc(hc);
		renderJson(c);
	}
	//查询所有可用于换车的车辆
	public void queryAllCanChangeCar(){
		List<CarInfo> carList = app.queryCarListCanRent(getPara("deptId"));
		c.setCarList(carList);
		renderJson(c);
	}
	//模糊查询 当前场站可换车车辆
	public void queryChangeCarByCarNum(){
		List<CarInfo> carList=new ArrayList<CarInfo>();
		carList=CarInfo.dao.queryCarVague(getPara("carNum"),getPara("deptId"));
		c.setCarList(carList);
		renderJson(c);
	}
	public void changeCarSendCar(){
		String km=getPara("km");
		String contractId=getPara("contractId");
		String carNum=getPara("carNum");
		String deptId=getPara("deptId");
		String backTime=getPara("backTime");
		String empName=Employee.dao.findEmpNameById(getPara("empId"));
		boolean result = app.changeCarSuccess(contractId, carNum, km, deptId, backTime,empName);
		System.out.println(km+" "+contractId+" "+carNum+" "+deptId+" "+backTime);
		if(result){
			c.setCode("000");
			c.setMsg("操作成功");
		}else{
			c.setCode("111");
			c.setMsg("操作失败");
		}
		renderJson(c);
	}
	public void userIndex(){
		String empId = getPara("empId");
		Employee e = Employee.dao.findById(empId);
		Dept dept = Dept.deptDao.findById(e.getBigDecimal("deptid"));
		c.setEmp(e);
		c.setDept(dept);
		renderJson(c);
	}
}
